package com.xdf.ecommerce.controller;

import com.xdf.ecommerce.entity.EcommerceGoods;
import com.xdf.ecommerce.goods.GoodsInfo;
import com.xdf.ecommerce.service.IGoodsService;
import com.xdf.ecommerce.service.async.AsyncTaskManager;
import com.xdf.ecommerce.vo.AsyncTaskInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author chanchaw
 * @create 2025-10-28 19:55
 */
@ApiModel(description = "异步执行的商品信息控制器")
@CrossOrigin
@RestController
@RequestMapping("/asyncGoods")
@RequiredArgsConstructor
public class AsyncGoodsController {
    private final IGoodsService service;
    private final AsyncTaskManager taskManager;

    @GetMapping("/test")
    public LocalDateTime test(){
        return LocalDateTime.now();
    }

    /**
     * 更新商品信息到DB，然后更新到缓存 redis 中
     * 返回异步任务对象
     * @param goodsInfos
     * @return
     */
    @PostMapping("/import")
    public AsyncTaskInfo importGoods(@RequestBody List<GoodsInfo> goodsInfos){
        return taskManager.submit(goodsInfos);
    }

    @ApiOperation(value = "查询状态", notes = "根据taskId查询获取异步任务信息对象", httpMethod = "GET")
    @GetMapping("/taskInfo")
    public AsyncTaskInfo getTaskInfo(@RequestParam String taskId){
        return taskManager.getAsyncTaskInfo(taskId);
    }
}
